/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;

import beans.StoreProductBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class StoreProductDAO extends AbstractDAO<StoreProductBean>{
  
    @Override
    public void insert(StoreProductBean storeProduct) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "INSERT INTO STORE_PRODUCT(productId,storeId,productInStock,maxProductInStock,minProductInStock,productPrice,productDiscount,productBarCode)values('" + storeProduct.getProductId() + "','" + storeProduct.getStoreId() + "','" + storeProduct.getProductInStock() + "','" + storeProduct.getMaxProductInStock() + "','" + storeProduct.getMinProductInStock() + "','" + storeProduct.getProductPrice() + "','" + storeProduct.getProductDiscount() + "','" + storeProduct.getProductBarCode() + "');";
            dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void delete(String condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "DELETE FROM store_product " + condition;
                dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void update(StoreProductBean storeProduct, String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "UPDATE store_product SET storeId='" + storeProduct.getStoreId() + "',productId='" + storeProduct.getProductId() + "',productInStock='" + storeProduct.getProductInStock() + "',maxProductInStock='" + storeProduct.getMaxProductInStock() + "',minProductInStock='" + storeProduct.getMinProductInStock() + "',productPrice='" + storeProduct.getProductPrice() + "',productDiscount='" + storeProduct.getProductDiscount() + "',productBarCode='" + storeProduct.getProductBarCode() + "' " + condition;
            dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                 dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    protected ArrayList<StoreProductBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE_PRODUCT " + condition;
            ArrayList<StoreProductBean> storeProductBeanList = null;
            storeProductBeanList = createStoreProductBeanList(dataBaseManager.executeQuery(sqlStatement));
            return storeProductBeanList;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    protected StoreProductBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE_PRODUCT " + condition;
            StoreProductBean storeProductBean = null;
            storeProductBean = createStoreEmployeBean(dataBaseManager.executeQuery(sqlStatement));
            return storeProductBean;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

private ArrayList<StoreProductBean> createStoreProductBeanList(ResultSet queryResult) throws DAOException{
        try {
                ArrayList<StoreProductBean> storeEmployeeBeanList = new ArrayList();
                while (queryResult.next()) {
                    storeEmployeeBeanList.add((StoreProductBean) beansGenerator.generateBean(queryResult, StoreProductBean.class));
                }
                return storeEmployeeBeanList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private StoreProductBean createStoreEmployeBean(ResultSet queryResult) throws DAOException{
        try {
            moveQueryResultToBegin(queryResult);
            return (StoreProductBean) beansGenerator.generateBean(queryResult, StoreProductBean.class);
        } catch (ORMException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        }
    }


     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }

}
